package com.wwy.arithmetic.suanfa;

import java.util.*;

/**
 * 重复元素2 判断数组中两个重复元素之前相差是否大于指定的值
 */
public class ChongFuYuanSuTwo {

    public static void main(String[] args) {
        int[] nums = {1, 0, 1, 1};
        boolean judge = judge(nums, 1);
        System.out.println("judge = " + judge);
    }

    static boolean judge(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>(nums.length);
        if (nums.length <= 1) {
            return false;
        }
        for (int i = 0; i < nums.length; i++) {
            Integer integer = map.get(nums[i]);
            if (integer != null && k >= i - integer) {
                return true;
            } else {
                map.put(nums[i], i);
            }
        }
        return false;
    }

    static boolean judgeTwo(int[] nums, int k) {
        if (nums.length == 1) {
            return false;
        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = 1; j <= k; j++) {
                if (j + i < nums.length && nums[i] == nums[j + i]) {
                    return true;
                }
            }
        }
        return false;
    }

    //   12342    3
    static boolean containsNearbyDuplicate(int[] nums, int k) {
        Set<Integer> set = new HashSet<Integer>();
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            if (i > k) {
                set.remove(nums[i - k - 1]);
            }
            if (!set.add(nums[i])) {
                return true;
            }
        }
        return false;
    }


}
